use crate::dto::{
    excel::{ExcelDetail, ExcelList, ExcelModifyPayload},
    match_no_res_ok, match_ok, Res, ResObj,
};
use crate::service::excel_service;
use hypers::prelude::*;
pub struct Excel;

#[openapi(prefix = "/tool", name = "/excel", tag = "文档")]
impl Excel {
    /// 添加文档
    #[post(
        "/add",
        response(
            (status = 200,body=ResObj<String>,description ="添加文档")
        ),
    )]
    async fn post_add_excel(payload: Json<ExcelModifyPayload>, depot: &mut Request) -> Res<String> {
        let user_id = depot.get::<i32>("userId").unwrap();
        match_ok(
            excel_service::add_excel(
                payload.excel_name.clone(),
                payload.excel_data.clone(),
                user_id,
            )
            .await,
        )
    }

    /// 修改文档
    #[post(
        "/edit",
        response(
            (status = 200,body=ResObj<()>,description ="修改文档")
        ),
    )]
    async fn post_edit_excel(payload: Json<ExcelModifyPayload>) -> Res<()> {
        match_no_res_ok(
            excel_service::edit_excel(payload.excel_id.clone(), payload.excel_data.clone()).await,
        )
    }

    /// 查询个人的文档列表
    #[get(
        "/user/list",
        response(
            (status = 200,body=ResObj<Vec<ExcelList>>,description ="查询个人的文档列表")
        ),
    )]
    async fn get_excel_list_by_user_id(depot: &mut Request) -> Res<Vec<ExcelList>> {
        let user_id = depot.get::<i32>("userId").unwrap();
        match_ok(excel_service::excel_list_by_user_id(user_id).await)
    }

    /// 文档列表
    #[get(
        "/list",
        response(
            (status = 200,body=ResObj<Vec<ExcelList>>,description ="文档列表")
        ),
    )]
    async fn get_excel_list() -> Res<Vec<ExcelList>> {
        match_ok(excel_service::excel_list().await)
    }

    /// 文档详情
    #[get(
        "/:excel_id",
        response(
            (status = 200,body=ResObj<Option<ExcelDetail>>,description ="文档详情")
        ),
    )]
    async fn get_excel_detail_by_excel_id(excel_id: Path<String>) -> Res<Option<ExcelDetail>> {
        match_ok(excel_service::excel_detail(excel_id.0).await)
    }
}
